<template>
  <div v-loding="listloading" class="archives-container">
    <!-- 新增分类标题区 -->
    <div class="category-header">
      <img :src="imgUrl" class="card-bg" alt="分类背景" />
      <span class="category-name">{{ label }}</span>
      <span class="category-count">{{ "总计" + total + "篇" }}</span>
    </div>
    <!-- 博客列表 -->
    <div v-loading="listloading" class="blog-list">
      <div v-for="post in postList" :key="post.id" class="post-card" @click="gotoDetail(post.id)">
        <div class="post-image">
          <img :src="post.image" alt="文章封面" />
        </div>
        <div class="post-content">
          <h3 class="post-title">{{ post.title }}</h3>
          <p class="post-desc">{{ post.description }}</p>
          <div class="post-meta">
            <span class="post-date">{{ post.publishTime }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { DictDataPageQuery } from "@/api/system/dict-data";
import { ArchivesPageQuery, ArchivesPageVO } from "@/api/system/archives";
import BlogAPI from "@/api/blog/blog";
// 分页控制参数
const postList = ref<ArchivesPageVO[]>(); // 文章列表数据
const listloading = ref(false);
const total = ref(0);
// 路由参数获取
const route = useRoute();
const router = useRouter();

// 路由类型参数
const type = ref("");
const label = ref("");

const queryParams = reactive<ArchivesPageQuery>({
  pageNum: 1,
  pageSize: 10,
  type: type,
});
// 博客文章类型编码
const typeCode = "archives_type";
const imgUrl = ref("");
const queryCategoryParams = reactive<DictDataPageQuery>({
  pageNum: 1,
  pageSize: 10,
  dictCode: typeCode,
});
// 加载博客文章列表
function loadPosts() {
  listloading.value = true;
  BlogAPI.getArchivesPage(queryParams)
    .then((data) => {
      postList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      listloading.value = false;
    });
}

// 获取当前类型背景图片
function getImgUrlByType() {
  BlogAPI.getCategoriesPage(queryCategoryParams).then((data) => {
    data.list.forEach((element) => {
      if (element.value == type.value) imgUrl.value = element.remark as string;
    });
  });
}

function gotoDetail(id: Number) {
  router.push("/blog/detail?aid=" + id); // 跳转到 /detail 页面
}

// 初始化加载
onMounted(() => {
  getImgUrlByType();
  loadPosts();
});
watch(
  () => route.query,
  (query) => {
    type.value = query.type as string;
    label.value = query.label as string;
    if (query.type != undefined) {
      getImgUrlByType();
      loadPosts();
    }
  },
  { immediate: true, deep: true }
);
</script>

<style lang="scss" scoped>
.archives-container {
  background: #000;
  min-height: 100vh;
  padding: 80px 20px 40px; /* 顶部留出导航栏空间 */
}

.category-header {
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  position: relative;
  align-items: center;
  justify-content: center;
  height: 100px;
  max-width: 65%;
  margin: 0 auto 40px;
  background: #1a1a1a;
  border-radius: 12px;
  margin-bottom: 30px;

  .category-name {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-size: 1.2rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    font-weight: 600;
  }

  .category-count {
    position: absolute;
    top: 70%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-size: 1.2rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    font-weight: 600;
  }

  .card-bg {
    width: 100%;
    height: 100%;
    border-radius: 12px;
    object-fit: cover;
    filter: brightness(0.7);
  }
}

.blog-list {
  max-width: 65%;
  margin: 0 auto;
}

.post-card {
  display: flex;
  height: 9rem;
  background: #1a1a1a;
  border-radius: 12px;
  margin-bottom: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  transition: transform 0.3s;

  &:hover {
    transform: translateY(-5px);
  }

  .post-image {
    width: 45%;
    border-radius: 12px 0 0 12px;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .post-content {
    width: 55%;
    padding: 25px;
    position: relative;
  }

  .post-title {
    color: #fff;
    font-size: 1.4rem;
    margin-bottom: 15px;
  }

  .post-desc {
    color: #ccc;
    font-size: 0.95rem;
    line-height: 1.6;
    margin-bottom: 30px;
  }

  .post-meta {
    position: absolute;
    bottom: 25px;
    right: 25px;

    .post-date {
      color: #888;
      font-size: 0.85rem;
    }
  }
}
</style>
